<?php

namespace app\admin\controller\auth;

use app\admin\controller\Base;
use think\Db;

/**
 * 用户组管理
 * Class GroupController
 * @package app\admin\controller\auth
 */
class Group extends Base
{

    /**
     * 主页面
     * @return mixed
     */
    public function index()
    {
        return view();
    }

    /**
     * 列表
     * @return mixed
     */
    public function lists()
    {

        $map = array();
        $data = array();
        $post = $this->request->param();
        if (isset($post['keywork'])) {
            $map[] = ['name', 'like', '%' . $post['keywork'] . '%'];
        }
        //dump($map);
        $data['count'] = Db::name('auth_group')->where($map)->count();
        // echo Db::name('goods')->where($map)->fetchSql(true)->select();
        $data['data'] = Db::name('auth_group')->where($map)->order('id desc')->page(input("page"), input('limit'))->select();
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);

    }

    /**
     * 添加页面和添加操作
     * @return mixed
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            if (!isset($post['title'])) {
                $this->error('名称不能为空');
            }
            if (Db::name('auth_group')->insert($post) !== false) {
                $this->success('操作成功');
            }

            $this->error('添加失败');
        } else {
            return $this->fetch('edit');
        }
    }

    /**
     * 修改页面和修改操作
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
//            if (!isset($post['name'])) {
//                $this->error('名称不能为空');
//            }
            if (Db::name('auth_group')->update($post) !== false) {
                $this->success('操作成功');
            }

            $this->error('更新失败');
        } else {
            $id = $this->request->param('id', 0, 'intval');
            if (!$id) {
                exit('参数错误');
            }
            $info = Db::name('auth_group')->where('id', $id)->find();
            $this->assign('info', $info);

            return view();
        }
    }

    /**
     * 删除操作
     * @return json
     */
    public function delete()
    {
        $id = $this->request->param('id', 0, 'intval');
        if ($id <= 0) {
            $this->error('参数错误');
        }
        if (Db::name('auth_group')->delete($id) !== false) {
            Db::name('auth_group_access')->where('group_id', $id)->delete();
            $this->success('删除成功');
        }
        $this->error('删除失败');
    }


    public function getrule()
    {

        $id = $this->request->param('id', 0, 'intval');
        if (!$id || $id == 1) {
            exit('参数错误');
        }
        $info = Db::name('auth_group')->where('id', $id)->find();
        //已分配
        if (empty($info['rules'])) {
            $info['rules'] = 0;
        }
        $have_rule = Db::name('auth_rule')->where('id', 'in', $info['rules'])->field('id')->order('name asc')->select();
        // $no_rule   = Db::name('auth_rule')->where('id', 'not in', $info['rules'])->order('name asc')->select();
        $list['rule'] = Db::name('auth_rule')->order('name asc')->select();
        $list['have_rule'] = Db::name('auth_rule')->where('id', 'in', $info['rules'])->field('id')->order('name asc')->select();
        $list['have_rule'] = array_column($list['have_rule'], 'id');
        $data = [];
        $data['data'] = $list;
        if ($data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);


    }

    public function assigned()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            if (!$post['id'] || $post['id'] == 1) {
                $this->error('参数错误');
            } else {
                if (empty($post['list'])) {
                    $rule = '';
                    $this->error('参数错误');
                } else {
                    $list = array_column($post['list'], 'value');
                    $rule = implode(',', $list);
                    if (Db::name('auth_group')->where(['id' => $post['id']])->setField('rules', $rule) !== false) {
                        $this->success('设置成功');
                    } else {
                        $this->error('设置失败');
                    }
                }

            }

        } else {
            $id = $this->request->param('id', 0, 'intval');

            if (!$id || $id == 1) {
                $this->error('参数错误');
            } else {
                $this->assign('id', $id);
            }
            return view();

        }
    }

}